from rasa_sdk import Action, Tracker
from rasa_sdk.types import DomainDict
from rasa_sdk.forms import FormValidationAction
from rasa_sdk.executor import CollectingDispatcher
from typing import Any, Dict, List, Text, Optional
from rasa_sdk.events import (
    SlotSet,
    UserUtteranceReverted,
    ConversationPaused,
    EventType,
)

from urllib import parse,request
import json

class ActionReportWeather(Action):
    def name(self) -> Text:
        return "action_report_weather"    
    
    def run(self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> List[EventType]:
        city = tracker.get_slot('city')
        # header_dict = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'}
        # url='http://127.0.0.1:8444/weather'
        # req = request.Request(url='%s%s%s' % (url,'?',textmod),headers=header_dict)
        # result = request.urlopen(req)
        # result = result.read()
        date = tracker.get_slot('date')
        time = tracker.get_slot('time')
        result = {
            'intent': 'weather',
            'city': city,
            'date': date,
            'time': time
        }
        result = json.dumps(result)
        print("weather_running")
        return [SlotSet('weather_outline', result if result is not None else [])]

class ActionSearch(Action):
    def name(self):
        return "action_search"
    def run(self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> List[EventType]:
        search_info = tracker.get_slot('search_info')
        result = {
            'intent': 'search',
            'info': search_info,
        }
        result = json.dumps(result)
        return [SlotSet('search_result', result if result is not None else [])]

class ActionOpenApps(Action):
    def name(self):
        return "action_open_app"
    def run(self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> List[EventType]:
        textmod = tracker.get_slot('app')
        result = {
            'intent': 'open_app',
            'app': textmod,
        }
        result = json.dumps(result)
        print("open_running")
        return [SlotSet('search_result', result if result is not None else [])]

class ActionSearchMap(Action):
    def name(self):
        return "action_search_map"
    def run(self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> List[EventType]:
        textmod = tracker.get_slot('city')
        result = {
            'intent': 'search_map',
            'city': textmod,
        }
        result = json.dumps(result)
        print("map_running")
        return [SlotSet('search_result', result if result is not None else [])]        